package cn.iocoder.yudao.module.order.service.payorder;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.order.controller.admin.payorder.vo.*;
import cn.iocoder.yudao.module.order.dal.dataobject.payorder.PayOrderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.order.convert.payorder.PayOrderConvert;
import cn.iocoder.yudao.module.order.dal.mysql.payorder.PayOrderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.order.enums.ErrorCodeConstants.*;

/**
 * 支付订单 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PayOrderServiceImpl implements PayOrderService {

    @Resource
    private PayOrderMapper payOrderMapper;

    @Override
    public Integer createPayOrder(PayOrderCreateReqVO createReqVO) {
        // 插入
        PayOrderDO payOrder = PayOrderConvert.INSTANCE.convert(createReqVO);
        payOrderMapper.insert(payOrder);
        // 返回
        return payOrder.getId();
    }

    @Override
    public void updatePayOrder(PayOrderUpdateReqVO updateReqVO) {
        // 校验存在
        validatePayOrderExists(updateReqVO.getId());
        // 更新
        PayOrderDO updateObj = PayOrderConvert.INSTANCE.convert(updateReqVO);
        payOrderMapper.updateById(updateObj);
    }

    @Override
    public void deletePayOrder(Integer id) {
        // 校验存在
        validatePayOrderExists(id);
        // 删除
        payOrderMapper.deleteById(id);
    }

    private void validatePayOrderExists(Integer id) {
        if (payOrderMapper.selectById(id) == null) {
            throw exception(PAY_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public PayOrderDO getPayOrder(Integer id) {
        return payOrderMapper.selectById(id);
    }

    @Override
    public List<PayOrderDO> getPayOrderList(Collection<Integer> ids) {
        return payOrderMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PayOrderDO> getPayOrderPage(PayOrderPageReqVO pageReqVO) {
        return payOrderMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PayOrderDO> getPayOrderList(PayOrderExportReqVO exportReqVO) {
        return payOrderMapper.selectList(exportReqVO);
    }

}
